package com.demo.sendmsg;

import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Hex;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class SenderReceiveHandler extends IoHandlerAdapter {
	public static String seqno="";
	public static String msgid="";
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// TODO Auto-generated method stub
		// super.messageReceived(session, message);
		/*byte[] byArr = (byte[]) message;
		System.out.print("dsp返回的响应 :");
		char[] chArr = Hex.encodeHex(byArr);
		String str="";
		int i = 0;
		for (char c : chArr) {
			System.out.print(String.valueOf(c).toUpperCase());
			if((i++)%2 != 0){
				System.out.print(" ");
			}str+=String.valueOf(c).toUpperCase();
			
		}
		FileWriter fw = new FileWriter("D:\\g7\\智能管车\\dsp1\\消息推送\\dsp.txt",true);
            fw.write(str);
            fw.write("\r\n");
            fw.flush();
//		if(str.length()==126){
//			seqno=str.substring(36, 40);
//		}
		if("8300".equals(str.substring(2, 6))){
			msgid=str.substring(2, 6);
			seqno=str.substring(22, 26);
		}
//		System.out.println("");
//		System.out.println("msg id="+str.substring(2, 6));
		System.out.println("");
		// session.write("mymina1设备收到消息再次发送消息");*/
		SimpleDateFormat formattera = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTimea = new Date();
	 	String dateStringa = formattera.format(currentTimea);
		byte[] byArr = (byte[]) message;
		//System.out.println("dsp应答的长度："+byArr.length);
		char[] chArr = Hex.encodeHex(byArr);
		String str="";
		for (char c : chArr) {
			str+=String.valueOf(c).toUpperCase();
		}
		String respone="";
		for(int i=0;i<str.length();i++){
			if(i%2==0){
				String tmp=str.substring(i,i+2);
				respone=respone+" "+tmp;
			}
		}
		respone=respone.substring(1,respone.length());
		//System.out.println();
		//System.out.println(dateStringa+"  响应："+respone);
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionClosed(session);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		// TODO Auto-generated method stub
		super.sessionIdle(session, status);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {

		// super.messageSent(session, message);
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {

		super.sessionCreated(session);
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}

}